package com.example.idachuappone.person.method;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.example.idachuappone.R;
import com.example.idachuappone.promptly.entity.Addresses;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.CusProSmallBgDialog;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class AddressMethod {

	public static interface SetAddress {
		void onFail();

		void onSuccess(String str);
	}

	static class AnonymousClass_1 extends RequestCallBack<String> {
		private final/* synthetic */Addresses val$addresses;
		private final/* synthetic */Context val$context;
		private final/* synthetic */CusProSmallBgDialog val$cusProDialog;
		private final/* synthetic */SetAddress val$setAddress;

		AnonymousClass_1(CusProSmallBgDialog cusProSmallBgDialog, Context context, Addresses addresses,
				SetAddress setAddress) {
			this.val$cusProDialog = cusProSmallBgDialog;
			this.val$context = context;
			this.val$addresses = addresses;
			this.val$setAddress = setAddress;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			this.val$cusProDialog.dismiss();
			Log.e("a", NetUtil.unescapeUnicode(responseInfo.result));
			try {
				if (new JSONObject(NetUtil.unescapeUnicode(responseInfo.result)).getInt("code") == 10000) {
					PrefUtil.getInstance(this.val$context).setFimallyLocateCoor(
							new StringBuilder(String.valueOf(this.val$addresses.getLng())).append(",")
									.append(this.val$addresses.getLat()).toString());
					PrefUtil.getInstance(this.val$context).setArea(this.val$addresses.getArea());
					PrefUtil.getInstance(this.val$context).setXiaoQu(this.val$addresses.getCommunity());
					PrefUtil.getInstance(this.val$context).setDoor(this.val$addresses.getDoor_number());
					PrefUtil.getInstance(this.val$context).setPhone(this.val$addresses.getPhone());
					PrefUtil.getInstance(this.val$context).setName(this.val$addresses.getName());
					PrefUtil.getInstance(this.val$context).setUserAddressId(this.val$addresses.getId());
					this.val$setAddress.onSuccess("");
				}
			} catch (JSONException e) {
				e.printStackTrace();
				MainToast.show(this.val$context, "\u6682\u65e0\u6570\u636e", 0);
				this.val$setAddress.onFail();
			}
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			this.val$cusProDialog.dismiss();
			MainToast.show(this.val$context, this.val$context.getResources().getString(R.string.netFail), 0);
			this.val$setAddress.onFail();
		}
	}

	static class AnonymousClass_2 extends RequestCallBack<String> {
		private final/* synthetic */CusProSmallBgDialog val$cusProDialog;
		private final/* synthetic */SetAddress val$setAddress;

		AnonymousClass_2(CusProSmallBgDialog cusProSmallBgDialog, SetAddress setAddress) {
			this.val$cusProDialog = cusProSmallBgDialog;
			this.val$setAddress = setAddress;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onSuccess(responseInfo.result);
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onFail();
		}
	}

	static class AnonymousClass_3 extends RequestCallBack<String> {
		private final/* synthetic */CusProSmallBgDialog val$cusProDialog;
		private final/* synthetic */SetAddress val$setAddress;

		AnonymousClass_3(CusProSmallBgDialog cusProSmallBgDialog, SetAddress setAddress) {
			this.val$cusProDialog = cusProSmallBgDialog;
			this.val$setAddress = setAddress;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onSuccess(responseInfo.result);
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onFail();
		}
	}

	static class AnonymousClass_4 extends RequestCallBack<String> {
		private final/* synthetic */CusProSmallBgDialog val$cusProDialog;
		private final/* synthetic */SetAddress val$setAddress;

		AnonymousClass_4(CusProSmallBgDialog cusProSmallBgDialog, SetAddress setAddress) {
			this.val$cusProDialog = cusProSmallBgDialog;
			this.val$setAddress = setAddress;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onSuccess(responseInfo.result);
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			this.val$cusProDialog.dismiss();
			this.val$setAddress.onFail();
		}
	}

	static class AnonymousClass_5 extends RequestCallBack<String> {
		private final/* synthetic */SetAddress val$setAddress;

		AnonymousClass_5(SetAddress setAddress) {
			this.val$setAddress = setAddress;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			this.val$setAddress.onSuccess(responseInfo.result);
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			this.val$setAddress.onFail();
		}
	}

	public static void setAddress(Context context, Addresses addresses, CusProSmallBgDialog cusProDialog,
			SetAddress setAddress) {
		cusProDialog.setMsg("\u8bbe\u7f6e\u5730\u5740\u4e2d...");
		cusProDialog.show();
		Map<String, String> map = new HashMap();
		map.put("id", addresses.getId());
		NetUtil.post(context, Constant.SET_ADDRESSS, map, new AnonymousClass_1(cusProDialog, context, addresses,
				setAddress));
	}

	public static void updateAddress(Context context, Addresses addresses, CusProSmallBgDialog cusProDialog,
			SetAddress setAddress) {
		Map<String, String> map = new HashMap();
		map.put("id", addresses.getId());
		map.put("area", addresses.getArea());
		map.put("community", addresses.getCommunity());
		map.put("door_number", addresses.getDoor_number());
		map.put("name", addresses.getName());
		map.put("phone", addresses.getPhone());
		NetUtil.post(context, Constant.UPDATE_ADDRESSS, map, new AnonymousClass_2(cusProDialog, setAddress));
	}

	public static void addAddress(Context context, Addresses addresses, CusProSmallBgDialog cusProDialog,
			SetAddress setAddress) {
		Map<String, String> map = new HashMap();
		map.put("area", addresses.getArea());
		map.put("community", addresses.getCommunity());
		map.put("door_number", addresses.getDoor_number());
		map.put("name", addresses.getName());
		map.put("phone", addresses.getPhone());
		NetUtil.post(context, Constant.ADD_ADDRESSS, map, new AnonymousClass_3(cusProDialog, setAddress));
	}

	public static void getAddressList(Context context, CusProSmallBgDialog cusProDialog, SetAddress setAddress) {
		cusProDialog.setMsg("\u8f7d\u5165\u4e2d...");
		cusProDialog.show();
		NetUtil.get(context, Constant.USER_INFO, 100, new AnonymousClass_4(cusProDialog, setAddress));
	}

	public static void getLocation(Context context, String addressname, SetAddress setAddress) {
		NetUtil.get(context, "http://api.map.baidu.com/geocoder/v2/?address=" + addressname
				+ "&output=json&ak=C1bdec719d928d8581906fc068c35bee", 100, new AnonymousClass_5(setAddress));
	}
}
